<?php
namespace fast;

use fast\Http;
use think\Cache;

class Woapi {

    protected $domain = 'http://wo-api.uni-ubi.com';
    protected $appKey = '3ED5F63909144DF1B56DA3B2D494FA34';
    protected $appSecret = '9C836ABDD9024E139138A5A11EAAEDAD';
    protected $projectId = 'E52BC1CD19554E41A325DBDFD494BFAA';
    protected $source = '000000';

    //授权
    public function auth() {
        $url = $this->domain . "/v1/{$this->projectId}/auth";

        $milliseconds = microtime(true) * 1000;
        $milliseconds = substr($milliseconds, 0, 13);
        $sign = md5($this->appKey.$milliseconds.$this->appSecret);
        
        $headers = [
            "appKey:{$this->appKey}",
            "timestamp:{$milliseconds}",
            "sign:{$sign}"
        ];

        $result = Http::get($url, [], [ CURLOPT_HTTPHEADER => $headers ]);
        $json = json_decode($result, true);
        if($json['result'] != 1) {
            ajaxerror("授权失败" . $json['msg']);
        }

        Cache::set($this->appKey, $json['data'], 86400);
        return $json['data'];
    }


    /** 创建设备
     * name 设备名称
     * tag 设备标签
     * source 设备来源
     * deviceNo 设备序列号
     * addition 扩展字段
     */
    public function deviceCreate($name, $tag, $deviceNo, $forceEmptyDevice, $password) {
        $url = $this->domain . "/v2/device/create";

        $token = Cache::get($this->appKey);
        if(!$token) {
            $token = $this->auth();
        }

        $headers = [
            "Content-Type: application/json",
            "token:{$token}",
            "projectGuid:{$this->projectId}"
        ];

        $params = ['name' => $name, 'tag' => $tag, 'sceneGuid' => '', 'source' => $this->source, 'deviceNo' => $deviceNo, 'addition' => "{'recType':'1'}", 'forceEmptyDevice' => $forceEmptyDevice == 1 ? true : false];
        if($password) {
            $params['password'] = $password;
        }

        $result = Http::post(
            $url, 
            json_encode($params, true), 
            [ CURLOPT_HTTPHEADER => $headers ]
        );

        $json = json_decode($result, true);
        if($json['result'] != 1) {
            ajaxerror("创建设备失败" . $json['msg']);
        }

        return $json['data'];
    }

    /** 更新设备
     * name 设备名称
     * tag 设备标签
     * source 设备来源
     * deviceNo 设备序列号
     * addition 扩展字段
     */
    public function deviceUpdate($name, $tag, $deviceNo) {
        $url = $this->domain . "/v2/device/update";

        $token = Cache::get($this->appKey);
        if(!$token) {
            $token = $this->auth();
        }

        $headers = [
            "Content-Type: application/json",
            "token:{$token}",
            "projectGuid:{$this->projectId}"
        ];

        $params = ['name' => $name, 'tag' => $tag, 'source' => $this->source, 'deviceNo' => $deviceNo, 'addition' => "{'recType':'1'}"];

        $result = Http::post(
            $url, 
            json_encode($params, true), 
            [ CURLOPT_HTTPHEADER => $headers ]
        );

        $json = json_decode($result, true);
        if($json['result'] != 1) {
            ajaxerror("更新设备失败" . $json['msg']);
        }

        return $json['data'];
    }

    /** 删除设备
     * deviceNo 设备序列号
     */
    public function deviceDelete($deviceNo) {
        $url = $this->domain . "/v2/device/delete";

        $token = Cache::get($this->appKey);
        if(!$token) {
            $token = $this->auth();
        }

        $headers = [
            "Content-Type: application/json",
            "token:{$token}",
            "projectGuid:{$this->projectId}"
        ];

        $params = ['source' => $this->source, 'deviceNo' => $deviceNo];

        $result = Http::post(
            $url, 
            json_encode($params, true), 
            [ CURLOPT_HTTPHEADER => $headers ]
        );

        $json = json_decode($result, true);
        if($json['result'] != 1) {
            ajaxerror("删除设备失败" . $json['msg']);
        }

        return $json['data'];
    }

    /** 设备列表
     * index 
     * length
     * name 设备名称
     * deviceNo 设备序列号
     */
    public function devicePage($index, $length, $name = '', $deviceNo = '') {
        $url = $this->domain . "/v2/device/page";

        $token = Cache::get($this->appKey);
        if(!$token) {
            $token = $this->auth();
        }
        
        $headers = [
            "Content-Type: application/json",
            "token:{$token}",
            "projectGuid:{$this->projectId}"
        ];

        $params = ['index' => $index, 'length' => $length];
        if($name) {
            $params['name'] = $name;
        }
        if($deviceNo) {
            $params['deviceNo'] = $deviceNo;
        }

        $result = Http::post(
            $url, 
            json_encode($params, true), 
            [ CURLOPT_HTTPHEADER => $headers ]
        );

        $json = json_decode($result, true);
        if($json['result'] != 1) {
            ajaxerror("删除设备失败" . $json['msg']);
        }

        return $json['data'];
    }

    /** 设备详情
     * deviceNo 设备序列号
     */
    public function deviceDetail($deviceNo) {
        $url = $this->domain . "/v2/device/detail";

        $token = Cache::get($this->appKey);
        if(!$token) {
            $token = $this->auth();
        }

        $headers = [
            "Content-Type: application/json",
            "token:{$token}",
            "projectGuid:{$this->projectId}"
        ];

        $params = ['deviceNo' => $deviceNo, 'source' => $this->source];

        $result = Http::post(
            $url, 
            json_encode($params, true), 
            [ CURLOPT_HTTPHEADER => $headers ]
        );

        $json = json_decode($result, true);
        if($json['result'] != 1) {
            ajaxerror("查询设备详情失败" . $json['msg']);
        }

        return $json['data'];
    }
    

    /** 人员创建
     * name 名称
     * phone 手机
     * tag 备注
     * cardNo 卡号
     * idCardNo 身份证
     * password 密码
     * addition 扩展字段
     */
    public function admitCreate($name, $phone, $tag, $cardNo, $idCardNo, $password, $addition) {
        $url = $this->domain . "/v2/admit/create";

        $token = Cache::get($this->appKey);
        if(!$token) {
            $token = $this->auth();
        }

        $headers = [
            "Content-Type: application/json",
            "token:{$token}",
            "projectGuid:{$this->projectId}"
        ];

        $params = ['name' => $name, 'phone' => $phone, 'tag' => $tag, 'cardNo' => $cardNo, 'idCardNo' => $idCardNo, 'password' => $password, 'addition' => $addition];

        $result = Http::post(
            $url, 
            json_encode($params, true), 
            [ CURLOPT_HTTPHEADER => $headers ]
        );

        $json = json_decode($result, true);
        if($json['result'] != 1) {
            ajaxerror("人员创建失败" . $json['msg']);
        }

        return $json['data'];
    }

    /** 人员更新
     * name 名称
     * phone 手机
     * tag 备注
     * cardNo 卡号
     * idCardNo 身份证
     * password 密码
     * addition 扩展字段
     */
    public function admitUpdate($admitGuid, $name, $phone, $tag, $cardNo, $idCardNo, $password, $addition) {
        $url = $this->domain . "/v2/admit/update";

        $token = Cache::get($this->appKey);
        if(!$token) {
            $token = $this->auth();
        }

        $headers = [
            "Content-Type: application/json",
            "token:{$token}",
            "projectGuid:{$this->projectId}"
        ];

        $params = ['admitGuid' => $admitGuid, 'name' => $name, 'phone' => $phone, 'tag' => $tag, 'cardNo' => $cardNo, 'idCardNo' => $idCardNo, 'password' => $password, 'addition' => $addition];

        $result = Http::post(
            $url, 
            json_encode($params, true), 
            [ CURLOPT_HTTPHEADER => $headers ]
        );

        $json = json_decode($result, true);
        if($json['result'] != 1) {
            ajaxerror("人员更新失败" . $json['msg']);
        }

        return $json['data'];
    }

    /** 人员删除
     * admitGuids 人员ids
     */
    public function admitDelete($admitGuids) {
        $url = $this->domain . "/v2/admit/delete";

        $token = Cache::get($this->appKey);
        if(!$token) {
            $token = $this->auth();
        }

        $headers = [
            "Content-Type: application/json",
            "token:{$token}",
            "projectGuid:{$this->projectId}"
        ];

        $params = ['admitGuids' => $admitGuids];

        $result = Http::post(
            $url, 
            json_encode($params, true), 
            [ CURLOPT_HTTPHEADER => $headers ]
        );

        $json = json_decode($result, true);
        if($json['result'] != 1) {
            ajaxerror("人员删除失败" . $json['msg']);
        }

        return $json['data'];
    }

    /** 人像注册
     * admitGuids 人员
     * url 人像地址
     */
    public function faceRegister($admitGuid, $imageUrl) {
        $url = $this->domain . "/v2/face/register";

        $token = Cache::get($this->appKey);
        if(!$token) {
            $token = $this->auth();
        }

        $headers = [
            "Content-Type: application/json",
            "token:{$token}",
            "projectGuid:{$this->projectId}"
        ];

        $params = ['admitGuid' => $admitGuid, 'url' => $imageUrl, 'base64' => '', 'faceTag' => ''];

        $result = Http::post(
            $url, 
            json_encode($params, true), 
            [ CURLOPT_HTTPHEADER => $headers ]
        );        

        $json = json_decode($result, true);
        if($json['result'] != 1) {
            ajaxerror("人像注册失败" . $json['msg']);
        }

        return $json['data'];
    }

    /** 人像删除
     * admitGuids 人员
     * faceGuid 人像
     */
    public function faceDelete($admitGuid, $faceGuid) {
        $url = $this->domain . "/v2/face/delete";

        $token = Cache::get($this->appKey);
        if(!$token) {
            $token = $this->auth();
        }

        $headers = [
            "Content-Type: application/json",
            "token:{$token}",
            "projectGuid:{$this->projectId}"
        ];

        $params = ['admitGuid' => $admitGuid, 'faceGuid' => $faceGuid];

        $result = Http::post(
            $url, 
            json_encode($params, true), 
            [ CURLOPT_HTTPHEADER => $headers ]
        );

        return json_decode($result, true);
    }

    /** 设备授权识别主体
     * deviceNo 设备号
     * admitGuids 人员
     */
    public function authDevice($deviceNo, $admitGuids) {
        $url = $this->domain . "/v2/auth/device";

        $token = Cache::get($this->appKey);
        if(!$token) {
            $token = $this->auth();
        }

        $headers = [
            "Content-Type: application/json",
            "token:{$token}",
            "projectGuid:{$this->projectId}"
        ];

        $permission = [
            "facePermission" => 2,
            "idCardFacePermission" => 1,
            "passwordPermission" => 1,
            "idCardPermission" => 1,
            "faceAndCardPermission" => 1,
            "fingerPermission" => 1
        ];

        $params = ['deviceNo' => $deviceNo, 'admitGuids' => $admitGuids, 'type' => '1', 'passTime' => '', 'permissionTime' => '', 'permission' => $permission];

        $result = Http::post(
            $url, 
            json_encode($params, true), 
            [ CURLOPT_HTTPHEADER => $headers ]
        );

        $json = json_decode($result, true);
        if($json['result'] != 1) {
            ajaxerror("设备授权识别失败" . $json['msg']);
        }

        return $json['data'];
    }
    
    /** 设备销权识别主体
     * deviceNo 设备号
     * admitGuids 人员
     */
    public function authDeviceRevoke($deviceNo, $admitGuids) {
        $url = $this->domain . "/v2/auth/device/revoke";

        $token = Cache::get($this->appKey);
        if(!$token) {
            $token = $this->auth();
        }

        $headers = [
            "Content-Type: application/json",
            "token:{$token}",
            "projectGuid:{$this->projectId}"
        ];

        $permission = [
            "facePermission" => 2,
            "idCardFacePermission" => 1,
            "passwordPermission" => 1,
            "idCardPermission" => 1,
            "faceAndCardPermission" => 1,
            "fingerPermission" => 1
        ];

        $params = ['deviceNo' => $deviceNo, 'admitGuids' => $admitGuids, 'type' => '1', 'passTime' => '', 'permissionTime' => '', 'permission' => $permission];

        $result = Http::post(
            $url, 
            json_encode($params, true), 
            [ CURLOPT_HTTPHEADER => $headers ]
        );

        return json_decode($result, true);
    }
}